package com.sinsoft.monitorDemo.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinsoft.monitorDemo.dao.MemInfoDao;
import com.sinsoft.monitorDemo.dao.ServerDao;
import com.sinsoft.monitorDemo.entity.MemoryInfo;
import com.sinsoft.monitorDemo.entity.ServerInfo;

@Controller
@RequestMapping("/MemInfo")
public class MemInfoController {
	/**
	 * 得到当前时间之前一段时间的的内存信息
	 * @return
	 */
	@RequestMapping(value = "/getMemInfoList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getMemInfoList(HttpServletRequest request){
		MemInfoDao dao = new MemInfoDao();
		String ip = request.getParameter("ip");
		List<MemoryInfo> memoryInfos = dao.getMemInfoList(ip);
		ResponseData responseData = new ResponseData(true, memoryInfos);
		return responseData;	
	}
	/**
	 * 得到最新的内存信息
	 * @return
	 */
	@RequestMapping(value = "/getMeminfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getNewMemInfo(HttpServletRequest request){
		MemInfoDao dao = new MemInfoDao();
		String ip = request.getParameter("ip");
		MemoryInfo memoryInfo = dao.getNewMemInfo(ip);
		ResponseData responseData = new ResponseData(true, memoryInfo);
		return responseData;	
	}
	/**
	 * 得到所有的内存信息
	 * @return
	 */
	@RequestMapping(value = "/getAllMeminfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getAllMemInfo(HttpServletRequest request){
		MemInfoDao dao = new MemInfoDao();
		ServerDao serverDao = new ServerDao();
		List<ServerInfo> serverInfos = serverDao.getAllServerInfo();
		Map<String, MemoryInfo> memInfos = new HashMap<String, MemoryInfo>();
		MemoryInfo newMemInfo;
		for (ServerInfo serverInfo : serverInfos) {
			String ip = serverInfo.getIp();
			if(ip.contains(",")){
				String[] ips = ip.split(",");
				for (int i = 0; i < ips.length; i++) {
					newMemInfo = dao.getNewMemInfo(ips[i]);
					memInfos.put(ips[i], newMemInfo);
				}
			}else {
				newMemInfo = dao.getNewMemInfo(ip);
				memInfos.put(ip, newMemInfo);
			}
			
		}
		ResponseData responseData = new ResponseData(true, memInfos);
		return responseData;	
	}
}
